package com.umgsai.thread.thread56;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Main4 {
	private static Timer timer = new Timer();
	private static int runCount = 0;
	static public class MyTask extends TimerTask{

		@Override
		public void run() {
			try {
				System.out.println("begin run," + new Date());
				Thread.sleep(5000);
				System.out.println("end run," + new Date());
				runCount++;
				if (runCount == 5) {
					timer.cancel();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	public static void main(String[] args) {
		try {
			MyTask task = new MyTask();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString = "2016-07-10 17:00:00";
			Date date = sdf.parse(dateString);
			System.out.println("main开始时间：" + new Date());
			timer.schedule(task, date, 3000);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
